# encoding: utf8

"""
    open函数中，参数newline设置对读写文本文件的影响

    # 写文件[demo.txt]， newline=None, 做转换 \\n --> \\r\\n
    写入内容：
    'line-1\\nline-2\\n'
    文件内容：
    line-1\\r\\n
    line-2\\r\\n

    >>> demo_write_newline(lines='line-1\\nline-2\\n', file='demo2.txt', newline=None)

    >>> demo_read_newline('demo2.txt', newline='')               # 不做转换, 读出原文本
    'line-1\\r\\nline-2\\r\\n'

    >>> demo_read_newline('demo2.txt', newline=None)             # 做转换 \\r-->\\n, \\r\\n --> \\n
    'line-1\\nline-2\\n'

    >>> demo_readlines_newline('demo2.txt', newline=None)        # 以通用模式分割行，判断同newline=''， 分割行后再转换行结束符为\\n
    ['line-1\\n', 'line-2\\n']
    >>> demo_readlines_newline('demo2.txt', newline='')          # 以通用模式分割行，不做转换. 遇到\\r\\n也判断为行结束符
    ['line-1\\r\\n', 'line-2\\r\\n']


    # 写文件， newline=None, 做转换 \\n --> \\r\\n
    文件内容：
    line-1\\r\\n
    line-2\\r\\n
    >>> demo_write_newline(lines='line-1\\r\\nline-2\\r\\n', file='demo.txt', newline=None)

    >>> demo_read_newline('demo.txt', newline='')               # 不做转换, 读出原文本
    'line-1\\r\\r\\nline-2\\r\\r\\n'
    >>> demo_read_newline('demo.txt', newline=None)             # 做转换 \\r-->\\n, \\r\\n --> \\n
    'line-1\\n\\nline-2\\n\\n'

    >>> demo_readlines_newline('demo.txt', newline=None)        # 以通用模式分割行，做转换. 判断同newline=''， 判断后再转换行结束符为\\n
    ['line-1\\n', '\\n', 'line-2\\n', '\\n']
    >>> demo_readlines_newline('demo.txt', newline='')          # 以通用模式分割行，不做转换. 遇到\\r判断为行结束符，再次遇到\\r\\n也判断为行结束符
    ['line-1\\r', '\\r\\n', 'line-2\\r', '\\r\\n']


    # 写文件， newline='', 不做转换
    文件内容：
    line-1\\r\\n
    line-2\\r\\n
    >>> demo_write_newline(lines='line-1\\r\\nline-2\\r\\n', file='demo.txt', newline='')

    >>> demo_read_newline('demo.txt', newline='')               # 不做转换, 三个值''、'\\r'、'\\n'的效果一样。读出文件的原文本
    'line-1\\r\\nline-2\\r\\n'
    >>> demo_read_newline('demo.txt', newline=None)             # 做转换 \\r\\n --> \\n
    'line-1\\nline-2\\n'

    >>> demo_readlines_newline('demo.txt', newline=None)        # 以通用模式分割行，做转换\\r\\n-->\\n
    ['line-1\\n', 'line-2\\n']
    >>> demo_readlines_newline('demo.txt', newline='')          # 以通用模式分割行，不做转换
    ['line-1\\r\\n', 'line-2\\r\\n']
    >>> demo_readlines_newline('demo.txt', newline='\\r')       # 以\\r分隔行, 不做转换
    ['line-1\\r', '\\nline-2\\r', '\\n']
    >>> demo_readlines_newline('demo.txt', newline='\\n')       # 以\\n分隔行, 不做转换
    ['line-1\\r\\n', 'line-2\\r\\n']


    # 做转换,
    文本文件内容：
    line-1\\r\\n
    line-2\\r\\n
    >>> demo_write_newline(lines='line-1\\r\\nline-2\\r\\n',\
                  file='demo.txt', newline='\\r')       # 做转换 \\n-->\\r

    >>> demo_read_newline('demo.txt', newline='')               # 不做转换, 三个值''、'\\r'、'\\n'的效果一样。读出文件的原文本
    'line-1\\r\\rline-2\\r\\r'
    >>> demo_read_newline('demo.txt', newline=None)             # 做转换 \\r-->\\n
    'line-1\\n\\nline-2\\n\\n'

    >>> demo_readlines_newline('demo.txt', newline=None)        # 以通用模式分割行，做转换\\r\\n-->\\n
    ['line-1\\n', '\\n', 'line-2\\n', '\\n']
    >>> demo_readlines_newline('demo.txt', newline='')          # 以通用模式分割行，不做转换
    ['line-1\\r', '\\r', 'line-2\\r', '\\r']
    >>> demo_readlines_newline('demo.txt', newline='\\r')       # 以\\r分隔行, 不做转换
    ['line-1\\r', '\\r', 'line-2\\r', '\\r']
    >>> demo_readlines_newline('demo.txt', newline='\\n')       # 以\\n分隔行, 不做转换
    ['line-1\\r\\rline-2\\r\\r']

"""


def demo_write_newline(lines='', file='demo.txt', newline=None, encoding='utf8'):
    with open(file, mode='w', newline=newline, encoding=encoding) as fp:
        fp.writelines(lines)


def demo_read_newline(file='demo.txt', newline=None, encoding='utf8'):
    with open(file, newline=newline, encoding=encoding) as fp:
        rs = fp.read()
    return rs


def demo_readlines_newline(file='demo.txt', newline=None, encoding='utf8'):
    with open(file, newline=newline, encoding=encoding) as fp:
        rs = fp.readlines()
    return rs
